//
//  MainTabBar.swift
//  LVWeibo
//
//  Created by lv on 15/6/23.
//  Copyright © 2015年 lv. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 重新调整按钮布局
        // 按钮的数量
        let buttonCount = 5
        let w = bounds.width / CGFloat(buttonCount)
        let h = bounds.height
        
        let rect = CGRectMake(0, 0, w, h)
        
        // 遍历子视图，调整按钮位置
        var index = 0
        for v in subviews{
            // 判断是否为UIControl
            if v is UIControl && !(v is UIButton){
                v.frame = CGRectOffset(rect, CGFloat(index) * w, 0)
                
                index += index == 1 ? 2 : 1
            }
        }
        
        // 设置撰写按钮的位置
        composeButton.frame = CGRectOffset(rect, w * 2, 0)
    }
    
    // 懒加载自定义tabBar(闭包)
    lazy var composeButton:UIButton = {
        let btn = UIButton()
        
        // 设置按钮在不同状态下的图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 闭包中需要使用self
        // 将btn添加到tabBar中
        self.addSubview(btn)
        return btn
    }()
}
